//
//  EndLiveViewController.m
//  sklive
//
//  Created by Macx on 2017/5/4.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "EndLiveViewController.h"

@interface EndLiveViewController ()

@property (nonatomic, strong) NSArray *shareArr;

@property (nonatomic, strong) NSDictionary *userInfo;

@end

@implementation EndLiveViewController {
    
    UILabel *_time;
    UILabel *_giftNum;
    UILabel *_viewerNum;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.userInfo = GetUserDefaule(USERINFO);
    
    [self buildUI];
    
    [self endLive];
    
}

#pragma mark ==========构建UI==========
- (void)buildUI {

    UIImageView *bgImage = [[UIImageView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:bgImage];
//    bgImage.image = [UIImage imageNamed:@"girl0.jpg"]
    [bgImage sd_setImageWithURL:[NSURL URLWithString:self.userInfo[@"face"]] placeholderImage:[UIImage imageNamed:@"placeHolder.png"]];
    bgImage.contentMode = UIViewContentModeScaleAspectFill;
    
    // 毛玻璃
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blur];
    effectView.frame = bgImage.bounds;
    [bgImage addSubview:effectView];
    
    UIView *darkBgImg = [[UIView alloc] initWithFrame:bgImage.bounds];
    [self.view addSubview:darkBgImg];
    darkBgImg.backgroundColor = [UIColor blackColor];
    darkBgImg.alpha = 0.4;
    
    UIImageView *headPic = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.center.x-SW(8) , SW(6), SW(4), SW(4))];
    [self.view addSubview:headPic];
    headPic.layer.cornerRadius = SW(8);
    headPic.layer.borderWidth = 1;
    headPic.layer.borderColor = [UIColor whiteColor].CGColor;
    headPic.layer.masksToBounds = YES;
    headPic.contentMode = UIViewContentModeScaleAspectFill;
//    headPic.image = [UIImage imageNamed:@"girl0.jpg"];
    [headPic sd_setImageWithURL:[NSURL URLWithString:self.userInfo[@"face"]] placeholderImage:[UIImage imageNamed:@"placeHolder.png"]];
    
    UILabel *userName = [[UILabel alloc] init];
    [self.view addSubview:userName];
//    userName.text = @"本.简爱";
    userName.text = self.userInfo[@"nickname"];
    userName.textAlignment = NSTextAlignmentCenter;
    userName.textColor = [UIColor whiteColor];
    userName.font = kUHSystemFontWithSize(13);
    CGSize userNameSize = [LZCTools textSizeWithText:userName.text font:userName.font];
    userName.frame = CGRectMake(self.view.center.x-userNameSize.width/2, headPic.y+headPic.height+5, userNameSize.width, userNameSize.height);
    
    UILabel *userID = [[UILabel alloc] init];
    [self.view addSubview:userID];
//    userID.text = @"ID号：9527";
    userID.text = [NSString stringWithFormat:@"ID号：%@", self.userInfo[@"user_id"]];
    userID.textAlignment = NSTextAlignmentCenter;
    userID.textColor = [UIColor whiteColor];
    userID.font = kUHSystemFontWithSize(13);
    CGSize userIDSize = [LZCTools textSizeWithText:userID.text font:userID.font];
    userID.frame = CGRectMake(self.view.center.x-userIDSize.width/2, userName.y+userName.height+5, userIDSize.width, userIDSize.height);
    
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(SW(4), userID.y+userID.height+SW(10), SW(2), 1)];
    [self.view addSubview:line1];
    line1.backgroundColor = [UIColor lightGrayColor];
    line1.alpha = 0.5;
    
    _time = [[UILabel alloc] init];
    [self.view addSubview:_time];
//    liveTime.text = @"直播时长：00:00:00";
//    liveTime.text = [NSString stringWithFormat:@"直播时长：%@", self.liveDuration];
    _time.textColor = UIMainColorRGB;
    _time.textAlignment = NSTextAlignmentCenter;
    _time.font = kUHSystemFontWithSize(14);
    CGSize liveTimeSize = [LZCTools textSizeWithText:@"x" font:_time.font];
    _time.frame = CGRectMake(0, line1.y+line1.height+10, SCREEN_WIDTH, liveTimeSize.height);
    
    
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(SW(4), _time.y+_time.height+10, SW(2), 1)];
    [self.view addSubview:line2];
    line2.backgroundColor = [UIColor lightGrayColor];
    line2.alpha = 0.5;

    UIView *gift = [self createHeaderSubViewColumn:@"星光值"];
    [self.view addSubview:gift];
//    ((UILabel *)(NSArray *)[gift subviews][0]).tag = 31;
    _giftNum = (UILabel *)(NSArray *)[gift subviews][0];
    gift.frame = CGRectMake(SW(4), line2.y+1+SW(10), SW(4), [LZCTools textSizeWithText:@"字数" font:kUHSystemFontWithSize(13)].height*2+10);
    
    UIView *audience = [self createHeaderSubViewColumn:@"观看人数"];
    [self.view addSubview:audience];
//    ((UILabel *)(NSArray *)[audience subviews][0]).tag = 32;
    _viewerNum = (UILabel *)(NSArray *)[audience subviews][0];
    audience.frame = CGRectMake(SW(2), line2.y+1+SW(10), SW(4), [LZCTools textSizeWithText:@"字数" font:kUHSystemFontWithSize(13)].height*2+10);
    
    
    UIButton *endBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:endBtn];
    [endBtn setTitle:@"返回首页" forState:UIControlStateNormal];
    [endBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    CGFloat btnHeight = [LZCTools textSizeWithText:@"字数" font:kUHSystemFontWithSize(14)].height+SW(20);
    endBtn.frame = CGRectMake(SW(10), self.view.bounds.size.height-btnHeight-SW(20), SCREEN_WIDTH-SW(5), btnHeight);
    endBtn.backgroundColor = RGBA(170, 170, 170, 0.5);
    endBtn.layer.cornerRadius = btnHeight/2;
    endBtn.layer.masksToBounds = YES;
    [endBtn addTarget:self action:@selector(endLiveAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
//    CGFloat imgY = 0.0;
//    CGSize textSize = [LZCTools textSizeWithText:@"字数" font:kUHSystemFontWithSize(13)];
//    self.shareArr = @[@"微信", @"朋友圈", @"QQ", @"微博", @"QQ空间"];
//    NSArray *imgArr = @[@"WeChat", @"live_end_friends", @"QQ", @"WeiBo", @"live_end_zone"];
//    CGFloat btnSize = (SCREEN_WIDTH-40)/5;
//    for (int i = 0; i < 5; i++) {
//        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20+btnSize*i, endBtn.y-20-textSize.height, btnSize, textSize.height)];
//        [self.view addSubview:label];
//        label.textColor = [UIColor lightGrayColor];
//        label.font = kUHSystemFontWithSize(13);
//        label.textAlignment = NSTextAlignmentCenter;
//        label.text = self.shareArr[i];
//        
//        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [self.view addSubview:btn];
//        btn.frame = CGRectMake(20+btnSize*i, endBtn.y-20-textSize.height/2-btnSize-5, btnSize, btnSize);
//        btn.tag = 11+i;
//        [btn setImage:[UIImage imageNamed:imgArr[i]] forState:UIControlStateNormal];
//        imgY = btn.y;
//        [btn addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
//
//    }
//    
//    
//    UIView *line3 = [[UIView alloc] initWithFrame:CGRectMake(10, imgY-20, SCREEN_WIDTH-20, 1)];
//    [self.view addSubview:line3];
//    line3.backgroundColor = [UIColor lightGrayColor];
//    line3.alpha = 0.5;
//    
//    UILabel *share = [[UILabel alloc] init];
//    [self.view addSubview:share];
//    share.text = @"分享直播至";
//    share.textAlignment = NSTextAlignmentCenter;
//    share.textColor = [UIColor whiteColor];
//    share.font = kUHSystemFontWithSize(14);
//    CGSize shareSize = [LZCTools textSizeWithText:share.text font:share.font];
//    share.frame = CGRectMake(self.view.center.x-shareSize.width/2, imgY-25-shareSize.height, shareSize.width, shareSize.height);
    
}

// 构建礼物和观看人数视图
- (UIView *)createHeaderSubViewColumn:(NSString *)column {
    
    UIView *bgView = [[UIView alloc] init];
    
    CGSize textSize = [LZCTools textSizeWithText:column font:kUHSystemFontWithSize(13)];
    
    UILabel *num = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SW(4), textSize.height)];
    [bgView addSubview:num];
    num.textColor = UIMainColorRGB;
    num.font = kUHSystemFontWithSize(13);
//    num.text = @"123";
    num.textAlignment = NSTextAlignmentCenter;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, textSize.height+10, SW(4), textSize.height)];
    [bgView addSubview:label];
    label.textColor = UIMainColorRGB;
    label.font = kUHSystemFontWithSize(13);
    label.text = column;
    label.textAlignment = NSTextAlignmentCenter;
    
    return bgView;
}


#pragma mark ==========响应方法==========
/**
 分享响应
 */
- (void)shareAction:(UIButton *)sender {
    
//    UITapGestureRecognizer *tapRecognizer = (UITapGestureRecognizer *)sender;
    NSLog (@"%ld", sender.tag);
    
    switch (sender.tag) {
        case 11:
        {
             NSLog(@"%@被点击", self.shareArr[0]);
        }
            break;
        case 12:
        {
            NSLog(@"%@被点击", self.shareArr[1]);
        }
            break;
        case 13:
        {
            NSLog(@"%@被点击", self.shareArr[2]);
        }
            break;
        case 14:
        {
            NSLog(@"%@被点击", self.shareArr[3]);
        }
            break;
        case 15:
        {
            NSLog(@"%@被点击", self.shareArr[4]);
        }
            break;
            
        default:
            break;
    }

}

/**
 请求后台结束直播
 */
- (void)endLive {
    
    [[NetworkTool sharedTool] requestWithName:@"删除直播" URLString:APICreateLives parameters:nil method:@"DELETE" callBack:^(id responseObject) {
        
        NSDictionary *dict = responseObject[@"data"];
        
        int dur = [dict[@"duration"] intValue];
        
        int h = (int)dur/3600;
        int m = (int)(dur - h *3600)/60;
        int s = (int)dur%60;
        _time.text = [NSString stringWithFormat:@"直播时长：%02d:%02d:%02d", h, m, s];
        _viewerNum.text = [NSString stringWithFormat:@"%@", dict[@"viewer_num"]];
        _giftNum.text = [NSString stringWithFormat:@"%@", dict[@"stars_count"]];
        
    }];
}

/**
 结束直播
 */
- (void)endLiveAction:(id)sender {
//  只用两行代码就可以比较完美的解决问题。第一行代码让B的视图变为透明（由于父视图变为透明，任何B的子视图都会透明）。这样的效果将是C的视图从A的视图上滑出。
//     第二行代码中连续获取了两次presentingViewController，其实就是A，不过这使得A视图控制器中不用添加任何代码，从而解决了耦合的问题。
    self.presentingViewController.presentingViewController.view.alpha = 0;
    NSLog(@"结束直播，返回首页");
   [self.presentingViewController.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
	
	//[[self.navigationController popViewControllerAnimated:YES].navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
